package com.osell.selection.ui.product;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.activity.ImagePreviewActivity;
import com.osell.selection.data.network.entity.ProductDetail;
import com.osell.selection.ui.base.BaseFragment;
import com.osell.selection.widget.AutoViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class ProductHeadFragment extends BaseFragment {

    @BindView(R.id.vp_product_image)
    AutoViewPager vp_product_image;
    @BindView(R.id.tv_product_image_indicator)
    TextView tv_product_image_indicator;
    @BindView(R.id.tv_product_price)
    TextView tv_product_price;
    @BindView(R.id.tv_product_old_price)
    TextView tv_product_old_price;
    @BindView(R.id.tv_product_name)
    TextView tv_product_name;
    @BindView(R.id.tv_product_supplier_name)
    TextView tv_product_supplier_name;

    private Unbinder mUnBinder;

    private ProductDetail mProductDetail;

    public static ProductHeadFragment newInstance(ProductDetail productDetail) {
        ProductHeadFragment fragment = new ProductHeadFragment();
        Bundle args = new Bundle();
        args.putSerializable("product_detail", productDetail);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        if (args != null) {
            mProductDetail = (ProductDetail) args.getSerializable("product_detail");
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.product_detail_head, container, false);
        mUnBinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        String temp = mProductDetail.currency + mProductDetail.price;
        tv_product_price.setText(temp);
        temp = mProductDetail.currency + mProductDetail.oldPrice;
        tv_product_old_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        tv_product_old_price.setText(temp);
        tv_product_name.setText(mProductDetail.name);
        tv_product_supplier_name.setText(mProductDetail.storeName);

        List<String> images = new ArrayList<>();
        for (ProductDetail.GalleryImage item : mProductDetail.images) {
            images.add(item.imageUrl);
        }
        vp_product_image.setAdapter(new ProductImageAdapter(images));
        vp_product_image.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                String indicator = (position + 1) + "/" + mProductDetail.images.size();
                tv_product_image_indicator.setText(indicator);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        String indicator = 1 + "/" + mProductDetail.images.size();
        tv_product_image_indicator.setText(indicator);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnBinder.unbind();
    }

    public static class ProductImageAdapter extends PagerAdapter {

        private List<View> mRecycledViews = new ArrayList<>();

        private List<String> mData;

        ProductImageAdapter(List<String> data) {
            this.mData = data;
        }

        @Override
        public int getCount() {
            return mData == null ? 0 : mData.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((View) object);
            mRecycledViews.add((View) object);
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, final int position) {
            ImageView view;
            if (mRecycledViews.isEmpty()) {
                view = new ImageView(container.getContext());
                view.setScaleType(ImageView.ScaleType.CENTER_CROP);
            } else {
                view = (ImageView) mRecycledViews.get(0);
                mRecycledViews.remove(0);
            }
            String item = mData.get(position);
            Glide.with(container.getContext())
                    .load(item)
                    .centerCrop()
                    .into(view);
            view.setOnClickListener(v -> {
                Intent intent = new Intent(v.getContext(), ImagePreviewActivity.class);
                Bundle bundle = new Bundle();
                bundle.putStringArray("PHOTOES", mData.toArray(new String[]{}));
                bundle.putInt("position", position);
                intent.putExtras(bundle);
                v.getContext().startActivity(intent);
            });
            container.addView(view);
            return view;
        }
    }

}
